package minh.bac.wcbrazil2014.favorite;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.adapter.FavoriteListViewAdapter;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.object.FavoriteObj;
import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class ListViewTeamFM extends Fragment{
	
	ListView listView;
	FavoriteListViewAdapter adapter;
	FavoriteObj favoriteObj;
	ArrayList<FootBallTeamObj> footBallTeamObjs;
	private MySQLiteManager mMySQLiteManager;
	ArrayList<FavoriteObj> favoriteObjs;
	DisplayImageOptions displayImageOptions;
	Button buttonMore;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.favorite_listview, container, false);
		listView = (ListView) rootView.findViewById(R.id.favorite_listview);
		buttonMore = (Button) rootView.findViewById(R.id.favoritelv_moreteam_btn);
		mMySQLiteManager = new MySQLiteManager(getActivity());
		footBallTeamObjs = mMySQLiteManager.getFavoriteTeam();
		favoriteObjs = new ArrayList<FavoriteObj>();
		displayImageOptions = new DisplayImageOptions.Builder()
		.cacheInMemory(false)
		.cacheOnDisc(true)		
		.resetViewBeforeLoading(false)
		.bitmapConfig(Bitmap.Config.RGB_565)										
		.showImageForEmptyUri(R.drawable.ic_emblem_club_placeholder)
		.showImageOnFail(R.drawable.ic_emblem_club_placeholder)		
		.imageScaleType(ImageScaleType.EXACTLY)										
		.build();
		//
		
		LoadData();
		adapter = new FavoriteListViewAdapter(getActivity(), R.layout.favorite_item, favoriteObjs, displayImageOptions);		
		//
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				FavoriteFragment.switchFragment(R.id.container_favorite, FavoriteChildFM.newInstance(favoriteObjs.get(position).getFootBallTeamObj().getDoiBong(),favoriteObjs.get(position).getImgLink()), true, WCTAG.TAGFM_FAVORITE_CHILD);
			}
			
		});
		buttonMore.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FavoriteFragment.switchFragment(R.id.container_favorite, new ChooseFavoriteTeamFM(), true, null);
			}
		});
		return rootView;
	}
	public void LoadData()
	{
		for(int i = 0; i < footBallTeamObjs.size(); i++ )
		{
			ArrayList<MatchScheduleObj> arrayList = new ArrayList<MatchScheduleObj>();
			ArrayList<VongBangObj> temp = mMySQLiteManager.getVongBangByName(footBallTeamObjs.get(i).getDoiBong());
			if(temp != null || temp.size() != 0)
			{
				arrayList.addAll(temp);
			}
			ArrayList<VongDau> temp2 = mMySQLiteManager.getVongDauByName(footBallTeamObjs.get(i).getDoiBong());
			if(temp2 != null || temp2.size() != 0)
			{
				arrayList.addAll(temp2);
			}
			String imgLink = mMySQLiteManager.getLinkImgByName(footBallTeamObjs.get(i).getDoiBong());
			if(imgLink == null || imgLink.equals(""))
			{
				imgLink = mMySQLiteManager.getLinkImgByName2(footBallTeamObjs.get(i).getDoiBong());
			}
		
			if(arrayList!= null)
			{
				favoriteObjs.add(new FavoriteObj(footBallTeamObjs.get(i), arrayList, imgLink));
			}
		}
	}

}
